package com.complex.spring.advance.service.impl;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

class PrototypeA {
	public int i = 0;
	public void getInt() {
		System.out.println("i=" + i++);
	}
}

public class TestPrototype {
	PrototypeA prototypeA;

	public static void main(String[] args) {
		BeanFactory bf = new XmlBeanFactory(new ClassPathResource(
				"/bean/spring-service.xml"));
		// 说明scope ="prototype"起作用，两次请求不同的对象
		PrototypeA a = (PrototypeA) bf.getBean("prototypeA");
		a.getInt();
		a = (PrototypeA) bf.getBean("prototypeA");
		a.getInt();

		// 不使用方法注入时,两次结果不同,说明使用的是同一个对象
		TestPrototype type = (TestPrototype) bf.getBean("testPrototype");
		type.getPrototypeA().getInt();
		type = (TestPrototype) bf.getBean("testPrototype");
		type.getPrototypeA().getInt();

	}

	public PrototypeA getPrototypeA() {
		return prototypeA;
	}

	public void setPrototypeA(PrototypeA prototypeA) {
		this.prototypeA = prototypeA;
	}
}
